package com.hyjx.framework.component.mutidatasource;

import com.hyjx.framework.common.util.SpringBeanUtil;
import com.hyjx.framework.orm.dao.IBaseDao;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 多数据源适配器
 */
public class MutiDataSource implements ApplicationContextAware {
    private static IBaseDao dao = (IBaseDao) SpringBeanUtil.getBean("baseDao");
    private ConfigurableApplicationContext app;
    
    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        // TODO Auto-generated method stub
        this.app = (ConfigurableApplicationContext) arg0;
    }
    
    /**
     * 初始化方法
     */
    public void init() {
/*        List<DataSourceInfo> dsList = dao.queryObjectList("multidatasource.getAllDataSource", (Object) null);
        DynamicDataSource d = (DynamicDataSource) SpringBeanUtil.getBean("dynamicDataSource");
        Map<Object, Object> targetDataSources = new LinkedHashMap();
        Object baseds = SpringBeanUtil.getBean("dataSource");
        targetDataSources.put("dataSourceS", baseds);
        if (dsList != null) {
            DefaultListableBeanFactory acf = (DefaultListableBeanFactory) this.app.getAutowireCapableBeanFactory();
            String DATASOURCE_BEAN_CLASS = baseds.getClass().getName();
            for (int i = 0; i < dsList.size(); ++i) {
                DataSourceInfo ds = dsList.get(i);
                String beanKey = ds.getDatasourcename();
                String driverClassName = ds.getDriverClassName();
                String url = ds.getUrl();
                String username = ds.getUsername();
                String password = ds.getPassword();
                BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(DATASOURCE_BEAN_CLASS);
                bdb.getBeanDefinition().setAttribute("id", beanKey);
                bdb.addPropertyValue("driverClassName", driverClassName);
                bdb.addPropertyValue("url", url);
                bdb.addPropertyValue("username", username);
                bdb.addPropertyValue("password", password);
                bdb.addPropertyValue("maxActive", Integer.valueOf(300));
                bdb.addPropertyValue("maxWait", Integer.valueOf(300));
                bdb.addPropertyValue("removeAbandoned", "true");
                bdb.addPropertyValue("removeAbandonedTimeout", Integer.valueOf(60));
                bdb.addPropertyValue("logAbandoned", "true");
                bdb.addPropertyValue("timeBetweenEvictionRunsMillis", 3600000);
                bdb.addPropertyValue("minEvictableIdleTimeMillis", 3600000);
                acf.registerBeanDefinition(beanKey, bdb.getBeanDefinition());
                targetDataSources.put(beanKey, this.app.getBean(beanKey));
            }
        }
        
        d.setTargetDataSources(targetDataSources);
        d.afterPropertiesSet();*/
    }
    
    public void changeinit() {
        this.init();
    }
}